// 保存数据到sessionStorage


// 从sessionStorage获取数据


// 从sessionStorage删除保存的数据
sessionStorage.removeItem('key');


$(document).ready(function () {
    getBooks();
    getRateBooks();
    var username = getCookie('username');
    console.log(username);
    if (username != "null" && username != null) {
        $('#userName').text('你好'+username);
        $('#login').hide();
    } else
        $('#logout').hide()

});

function logout() {
    $.cookie('userId', null);
    $.cookie('username', null);
    location.reload();
}


function getCookie(name) {
    var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
    if (arr = document.cookie.match(reg))
        return unescape(arr[2]);
    else
        return null;
}


function getBooks(){
    if (sessionStorage.getItem('recBooks')) {
        if (getCookie('userId') == "null") {
            goPage(1);
            return;
        }
    }
    var uId = null;
    if (getCookie('userId' == null) || getCookie('userId' == "null")) {
        uId = 14;
    } else
        uId = getCookie('userId')
    $.ajax({
        type: "get",
        url: "http://120.78.183.13/brs/book/rec",
        data: {userId:uId},
        async: false,
        success:function (data) {
            sessionStorage.setItem('recBooks', JSON.stringify(data));
            console.log('返回数据成功');
            goPage(1);
        }
    });
}

function getRateBooks(){
    $.ajax({
        type: "get",
        url: "http://120.78.183.13/brs/book/randBooks",
        data: "",
        async: false,
        success:function (data) {
            $('.rand_books').empty();
            // totalPage = (data.bookCount + 6 - 1) / 6;
            res = data;
            var count = 0;
            for(each in data)
            {
                if (count === 3) {
                    break;
                }
                //    <div class="new_prod_box">
                //                             <a href="details.html">product name</a>
                //                             <div class="new_prod_bg">
                //                             <a href="details.html"><img src="images/thumb1.gif" alt="" title="" class="thumb" border="0" /></a>
                //                             </div>
                //                         </div>
                var bookItem = '<div class="new_prod_box">\n' +
                    '                        <a href="details.html?bookId=BOOKID">BOOKNAME</a>\n' +
                    '                        <div class="new_prod_bg">\n' +
                    '                            <span class="new_icon"><img src="images/promo_icon.gif" alt="" title=""/></span>\n' +
                    '                            <a href="details.html?bookId=BOOKID"><img src="COVER" alt="" title="" class="thumb"\n' +
                    '                                                        border="0"/></a>\n' +
                    '                        </div>\n' +
                    '                    </div>';
                bookItem = bookItem.replace('BOOKNAME', data[each]['bookName']).replace(/BOOKID/g, data[each]['bookId']).replace('COVER', data[each]['bookCover']);
                $('.rand_books').append(bookItem);
                count++;
            }
        }
    });
}


function goPage(page, cur) {
    $('.special_books').empty();
    // totalPage = (data.bookCount + 6 - 1) / 6;
    var curPage = parseInt(page)*4-1;
    var data = JSON.parse(sessionStorage.getItem('recBooks'));
    console.log(curPage);
    for(var i=curPage-3; i<curPage && i<data.length;i++)
    {
        //    <div class="new_prod_box">
        //                             <a href="details.html">product name</a>
        //                             <div class="new_prod_bg">
        //                             <a href="details.html"><img src="images/thumb1.gif" alt="" title="" class="thumb" border="0" /></a>
        //                             </div>
        //                         </div>
        var bookItem = '       <div class="feat_prod_box">\n' +
            '                    <div class="prod_img"><a href="details.html?bookId=BOOKID"><img src="COVER" alt="" title="" border="0"/></a></div>\n' +
            '                    <div class="prod_det_box">' +
            '<span class="special_icon"><img src="images/special_icon.gif" alt="" title=""/></span>\n' +
            '                        <div class="box_top"></div>\n' +
            '                        <div class="box_center">\n' +
            '                            <div class="prod_title">BOOKNAME</div>\n' +
            '                            <p class="details">SUM</p>\n' +
            '                            <a href="details.html?bookId=BOOKID" class="more">- 查看详情 -</a>\n' +
            '                            <div class="clear"></div>\n' +
            '                        </div>\n' +
            '                        <div class="box_bottom"></div>\n' +
            '                    </div>\n' +
            '                    <div class="clear"></div>\n' +
            '                </div>';
        bookItem = bookItem.replace('BOOKNAME', data[i].bookName).replace(/BOOKID/g, data[i]['bookId']).replace('COVER', data[i]['bookCover'])
            .replace('SUM', data[i].bookSummary);
        $('.special_books').append(bookItem);
    }

    if (cur === undefined) {

    }

}